﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;


namespace DiabetesMonitor
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        public Item item { get; set; }

        private void menuItemFileCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void menuItemMainInsert_Click(object sender, EventArgs e)
        {
            #region Checking for null values
            // Check Sugar level for null or int
            if (string.IsNullOrEmpty(txtSugarLevel.Text))
            {
                MessageBox.Show("Sugar level cannot be left blank!");
                return;
            }

            // Check Insulin # for null or int
            if (string.IsNullOrEmpty(txtSugarLevel.Text))
            {
                MessageBox.Show("Sugar level cannot be left blank!");
                return;
            }

            if (cboTimeOfDay.SelectedIndex < 0)
            {
                MessageBox.Show("Must select a Time of Day!");
                return;
            }

            if (cboMedication.SelectedIndex < 0)
            {
                MessageBox.Show("Must select a Medication!");
                return;
            }
            
            StoreLocal(item);
            #endregion
        }

        #region Store Localy
        public bool StoreLocal(DiabetesMonitor.Item i)
        {
            using (SqlCeConnection hConn = new SqlCeConnection(Utilities.GetDBConnectionString()))
            {
                hConn.Open();
                string sSQL = "";
                {
                    sSQL = "INSERT INTO tblRecords (id, InsulinLevel, SugarLevel, TimeOfDay, Medication) VALUES (@id, @InsulinLevel, @SugarLevel, @TimeOfDay, @Medication)";
                }

                try
                {
                    SqlCeCommand oCommand = new SqlCeCommand(sSQL, hConn);
                    oCommand.Parameters.Add(new SqlCeParameter("@id", SqlDbType.Int, 4, ParameterDirection.Input, false, 0, 0, "id", DataRowVersion.Current, i.id));
                    oCommand.Parameters.Add(new SqlCeParameter("@InsulinLevel", SqlDbType.NVarChar, 100, ParameterDirection.Input, false, 0, 0, "InsulinLevel", DataRowVersion.Current, i.InsulinLevel));
                    oCommand.Parameters.Add(new SqlCeParameter("@SugarLevel", SqlDbType.NVarChar, 300, ParameterDirection.Input, false, 0, 0, "SugarLevel", DataRowVersion.Current, i.SugarLevel));
                    oCommand.Parameters.Add(new SqlCeParameter("@TimeOfDay", SqlDbType.NVarChar, 100, ParameterDirection.Input, false, 0, 0, "TimeOfDay", DataRowVersion.Current, i.TimeOfDay));
                    oCommand.Parameters.Add(new SqlCeParameter("@Medication", SqlDbType.Int, 4, ParameterDirection.Input, false, 0, 0, "Medication", DataRowVersion.Current, i.Medication));

                    oCommand.ExecuteNonQuery();

                    hConn.Close();

                    return true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }   
        #endregion


        private void menuItemNewTimeOfDay_Click(object sender, EventArgs e)
        {
            frmNewTimeOfDay TimeOfDay = new frmNewTimeOfDay();
            TimeOfDay.Show();
        }

        private void menuItemNewMedication_Click(object sender, EventArgs e)
        {
            frmNewMedication NewMedication = new frmNewMedication();
            NewMedication.Show();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the '_Tables.tblMedication' table. You can move, or remove it, as needed.
            this.tblMedicationTableAdapter.Fill(this._Tables.tblMedication);
            // TODO: This line of code loads data into the '_Tables.tblTimeOfDay' table. You can move, or remove it, as needed.
            this.tblTimeOfDayTableAdapter.Fill(this._Tables.tblTimeOfDay);
            // Fill TimeOfDay list

            // Fill Medication list

        }
    }
}